package com.atguigui.leetcode1;

/**
 * 1638.统计只差一个字符的子串数目
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/3/27 9:08
 */
public class P1638CountSubstringsThatDifferByOneCharacter {
    public static void main(String[] args) {
        Solution solution = new P1638CountSubstringsThatDifferByOneCharacter().new Solution();
        // TO TEST
    }

    class Solution {
        public int countSubstrings(String s, String t) {
            int m = s.length(), n = t.length();
            int ans = 0;
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    //二者不同的字符个数
                    int diff = 0;
                    for (int k = 0; i + k < m && j + k < n; k++) {
                        diff += s.charAt(i + k) == t.charAt(j + k) ? 0 : 1;
                        if (diff > 1) {
                            //有一个不同后后续就无需遍历了
                            break;
                        } else if (diff == 1) {
                            ans++;
                        }
                    }
                }
            }

            return ans;
        }
    }
}
